home *** CD-ROM | disk | FTP | other *** search
- Path: druid.borland.com!usenet
- From: pete@borland.com (Pete Becker)
- Newsgroups: comp.lang.c++
- Subject: Re: templates, arguments and pointers
- Date: 19 Apr 1996 20:51:53 GMT
- Organization: Borland International
- Message-ID: <4l8ud9$esj@druid.borland.com>
- References: <31763ECE.352C@afrodite.kih.no>
- NNTP-Posting-Host: pbecker.borland.com
- Mime-Version: 1.0
- Content-Type: Text/Plain; charset=ISO-8859-1
- X-Newsreader: WinVN 0.99.5
-
- In article <31763ECE.352C@afrodite.kih.no>, paulken4@afrodite.kih.no says...
- >
- >Here is a problem we have:
- >
- >template <class D>
- > class Matrix : public Grid<D>
- > {
- > public:
- > Matrix();
- > Matrix(size_t rows, size_t cols);
- > etc.
- > }
- >
- >class tobject
- >{
- >public:
- > tobject();
- > ~tobject();
- >private:
- > Matrix<int> *pointer;
- >}
- >
- >tobject::tobject()
- >{
- > int i;
- > pointer = new Matrix<int>(23,23); // is this construct valid?
- >}
- >
- >My problem is in the constructor of tobject. The Matrix class
- >(are from a book, and all examples pertaining to it, just
- >instantiates the objects with those two arguments always, never
- >dealing with pointers to the Matrix class) demands (as it is
- >now) the two arguments, saying how wide and how high it should
- >be.
- >
- >The standard way to fill in a Matrix object is this:
- >
- >Matrix<int> A(2,2);
- >
- >A(0,0) = 1;
- >A(0,1) = 2;
- >
- >How would this work with pointers:
- >
- >*this->pointer(0,0) = 1;
- >*this->pointer(0,1) = 2;
- >
-
- Take two steps backward. You're too close to the code, and missing the
- overview. pointer is, indeed, a pointer. You need to dereference it to be able
- to talk about the object that it points to. So you need to say *pointer. Once
- you've done that, you can invoke its members, but you have to deal with
- operator precedence. That is, *pointer(0,0) says to apply the function call
- operator to pointer and dereference the result. Of course, that doesn't make
- sense. So tell the compiler to apply the function call operator to *pointer.
- Like this: (*pointer)(0,0). That's all you need.
- -- Pete
-
-